package wordcloudtag;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dataextraction.Entry;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;

public class WordCloudTagBean {
	private static WordCloudTagBean instance;
	private  WordCloudTagBean() {}

	public static WordCloudTagBean getInstance() {
		if(instance == null){
			instance = new WordCloudTagBean();
		}

		return instance;
	}

	public Map<String, WordCloudTag> createWordCloudTag(List<Entry> entries) {
		Map<String, WordCloudTag> mapWordCloudTag = new HashMap<String, WordCloudTag>();
		
		MaxentTagger tagger = new MaxentTagger("edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger");

		//    	5.	FW	FOREIGN WORD
		//    	7.	JJ	ADJECTIVE
		//    	8.	JJR	ADJECTIVE, COMPARATIVE
		//    	9.	JJS	ADJECTIVE, SUPERLATIVE
		//    	12.	NN	NOUN, SINGULAR OR MASS
		//    	13.	NNS	NOUN, PLURAL
		//    	14.	NNP	PROPER NOUN, SINGULAR
		//    	15.	NNPS	PROPER NOUN, PLURAL
		//    	23.	RP	PARTICLE
		//    	27.	VB	VERB, BASE FORM
		//    	28.	VBD	VERB, PAST TENSE
		//    	29.	VBG	VERB, GERUND OR PRESENT PARTICIPLE
		//    	30.	VBN	VERB, PAST PARTICIPLE
		//    	31.	VBP	VERB, NON-3RD PERSON SINGULAR PRESENT
		//    	32.	VBZ	VERB, 3RD PERSON SINGULAR PRESENT
		List<String> suffixes = Arrays.asList(new String[]{
				"_FW",
				"_JJ",
				"_JJR",
				"_JJS",
				"_NN",
				"_NNS",
				"_NNP",
				"_NNPS",
				"_RP",
				"_VB",
				"_VBD",
				"_VBG",
				"_VBN",
				"_VBP",
				"_VBZ"
		});


		for (Entry entry : entries) {
			String title = entry.getDocumentTitle();
			String tagged = tagger.tagString(title);

			String[] titleTaggedSplit = tagged.split(" ");
			for (String wordTagged : titleTaggedSplit) {

				for (String suffix : suffixes) {
					if(wordTagged.endsWith(suffix)){
						String word = wordTagged.replace(suffix, "");

						WordCloudTag wordCloudTag = new WordCloudTag(); 

						if(mapWordCloudTag.containsKey(word.toLowerCase())){
							wordCloudTag = mapWordCloudTag.get(word.toLowerCase());
						}

						wordCloudTag.setWord(word.toLowerCase());
						wordCloudTag.setQuantity(wordCloudTag.getQuantity() + 1);

						mapWordCloudTag.put(word.toLowerCase(),wordCloudTag);
					}
				}
			}
		}

		return mapWordCloudTag;
	}
}
